package com.baizhi.realm;

import com.baizhi.dao.AdminDao;
import com.baizhi.entity.Admin;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class DataSourceRealm extends AuthenticatingRealm {
    @Autowired
    private AdminDao adminDao;

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
//        获取token中的用户名
        String username = (String) token.getPrincipal();

//        从数据库中查询  得到信息
        Admin admin = adminDao.selectOne(username);
        if(admin!=null){
            SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(admin.getUserName(), admin.getPassword(), this.getName());
            return info;
        }
//        封装info
        return null;
    }
}
